/*
 * VentanaModificaArea.java
 *
 * Created on 8 de junio de 2007, 01:52 PM
 */

package everest3.materialBibliografico;

import everest3.Ayuda.MostrarAyuda; 
import java.awt.Frame; 
import java.awt.event.ActionEvent; 
import java.awt.event.FocusEvent; 
import java.awt.event.MouseEvent; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTextField;
import everest3.utils.Fecha;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.util.Vector;
import tablasYBasesdeDatos.JDBCAdapter;
import tablasYBasesdeDatos.S15WorkingBackspace;

/**
 * Clase que se encarga de la modificaci&oacute;n de las &Aacute;reas Tem&aacute;ticas
 * del sistema
 * @author JOSE LIBARDO MOLINA
 */
public class VentanaModificaArea extends javax.swing.JDialog {
    
    /** Creates new form VentanaModificaArea */
    public VentanaModificaArea(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
         conectar();
        initComponents();
         dimensionarPantalla();      
         BuscarArea();
    }
    
      public VentanaModificaArea(java.awt.Frame parent, boolean modal,String nombreUsuario) {
        super(parent, modal);
         conectar();
        initComponents();
          jTUsuario.setText(nombreUsuario);
         dimensionarPantalla();      
         BuscarArea();
    }
    /**
     * Meotod que centra la ventana en el monitor
     */
    	private void dimensionarPantalla()
  	{
  		Dimension pantalla, cuadro;
  		
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		cuadro = this.getSize();				
		
		this.setLocation(((pantalla.width - cuadro.width)/2), 
						(pantalla.height - cuadro.height)/2);
	}
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nombreArea = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        codigArea = new javax.swing.JComboBox();
        java.util.Vector datos = datbase.consultaCampos("select areacodigo from codarea order by areacodigo asc");
        for (int i=0; i< datos.size();i++)
        codigArea.addItem(df1.format(datos.elementAt(i)));
        new S15WorkingBackspace(codigArea);
        jCBtipoArea = new javax.swing.JComboBox();
        botonGuardar = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        botonAyuda = new javax.swing.JButton();
        jPEncabezado = new javax.swing.JPanel();
        JTFecha = new javax.swing.JTextField();
        jTUsuario = new javax.swing.JTextField();
        jLTitulo = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLlogo = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLEstado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("\u00c1reas Tematicas - Modificar");
        setResizable(false);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("C\u00f3digo de \u00e1rea"));
        jLabel1.setText("C\u00f3digo");

        jLabel2.setText("Nombre");

        nombreArea.setToolTipText("Nombre del \u00e1rea");
        nombreArea.setDocument(new everest3.utils.LimitadorCaracteres(nombreArea,100,2));
        nombreArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                nombreAreaMouseEntered(evt);
            }
        });
        nombreArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nombreAreaFocusGained(evt);
            }
        });

        jLabel3.setText("Tipo");

        codigArea.setEditable(true);
        codigArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                codigAreaMouseEntered(evt);
            }
        });
        codigArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codigAreaActionPerformed(evt);
            }
        });
        codigArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                codigAreaFocusGained(evt);
            }
        });

        jCBtipoArea.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Libro", "Tesis" }));
        jCBtipoArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jCBtipoAreaMouseEntered(evt);
            }
        });
        jCBtipoArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jCBtipoAreaFocusGained(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nombreArea, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addComponent(codigArea, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBtipoArea, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombreArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCBtipoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guardar \u00e1rea");
        botonGuardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonGuardarMouseEntered(evt);
            }
        });
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });
        botonGuardar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonGuardarFocusGained(evt);
            }
        });

        jButton4.setText("Cerrar");
        jButton4.setToolTipText("Cierra esta ventana");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton4MouseEntered(evt);
            }
        });
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jButton4FocusGained(evt);
            }
        });

        botonAyuda.setText("Ayuda");
        botonAyuda.setToolTipText("Buscar \u00e1rea");
        botonAyuda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonAyudaMouseEntered(evt);
            }
        });
        botonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAyudaActionPerformed(evt);
            }
        });
        botonAyuda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonAyudaFocusGained(evt);
            }
        });

        jPEncabezado.setLayout(null);

        jPEncabezado.setBackground(new java.awt.Color(204, 204, 204));
        JTFecha.setEditable(false);
        JTFecha.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        JTFecha.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        JTFecha.setDragEnabled(true);
        JTFecha.setEnabled(false);
        JTFecha.setFocusable(false);
        JTFecha.setText(FechaAct.obtenerFechaCompleta());
        jPEncabezado.add(JTFecha);
        JTFecha.setBounds(350, 10, 160, 20);

        jTUsuario.setEditable(false);
        jTUsuario.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTUsuario.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTUsuario.setDragEnabled(true);
        jTUsuario.setEnabled(false);
        jPEncabezado.add(jTUsuario);
        jTUsuario.setBounds(350, 40, 160, 20);

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLTitulo.setText("rea Tem\u00e1tica - Modificar");
        jPEncabezado.add(jLTitulo);
        jLTitulo.setBounds(125, 40, 220, 22);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 48));
        jLabel4.setText("\u00c1");
        jPEncabezado.add(jLabel4);
        jLabel4.setBounds(90, 10, 33, 58);

        jLlogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/logo.JPG")));
        jLlogo.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLlogo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPEncabezado.add(jLlogo);
        jLlogo.setBounds(0, 0, 85, 64);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/Barra.jpg")));
        jPEncabezado.add(jLabel5);
        jLabel5.setBounds(0, 0, 520, 65);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonAyuda))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, 517, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 517, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAyuda)
                    .addComponent(jButton4)
                    .addComponent(botonGuardar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-525)/2, (screenSize.height-302)/2, 525, 302);
    }// </editor-fold>//GEN-END:initComponents

    private void botonAyudaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonAyudaMouseEntered
// TODO add your handling code here:
         barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonAyudaMouseEntered

    private void botonAyudaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonAyudaFocusGained
// TODO add your handling code here:
         barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonAyudaFocusGained

    private void jButton4MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseEntered
// TODO add your handling code here:
         barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_jButton4MouseEntered

    private void jButton4FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jButton4FocusGained
// TODO add your handling code here:
         barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_jButton4FocusGained

    private void botonGuardarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonGuardarFocusGained
// TODO add your handling code here:
          barraEstado("Guarda la Modificaci\u00f3n del \u00c1rea Tematica en el Sistema");
    }//GEN-LAST:event_botonGuardarFocusGained

    private void botonGuardarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonGuardarMouseEntered
// TODO add your handling code here:
         barraEstado("Guarda la Modificaci\u00f3n del \u00c1rea Tematica en el Sistema");
    }//GEN-LAST:event_botonGuardarMouseEntered

    private void jCBtipoAreaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCBtipoAreaMouseEntered
// TODO add your handling code here:
         barraEstado("Selecciona el Tipo de \u00c1rea Tematica");
    }//GEN-LAST:event_jCBtipoAreaMouseEntered

    private void jCBtipoAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jCBtipoAreaFocusGained
// TODO add your handling code here:
         barraEstado("Selecciona el Tipo de \u00c1rea Tematica");
    }//GEN-LAST:event_jCBtipoAreaFocusGained

    private void nombreAreaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nombreAreaMouseEntered
// TODO add your handling code here:
          barraEstado("Digita el Nombre del \u00c1rea Tematica (Maximo 100 Caracteres)");
    }//GEN-LAST:event_nombreAreaMouseEntered

    private void nombreAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nombreAreaFocusGained
// TODO add your handling code here:
          barraEstado("Digita el Nombre del \u00c1rea Tematica (Maximo 100 Caracteres)");
    }//GEN-LAST:event_nombreAreaFocusGained

    private void codigAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_codigAreaFocusGained
// TODO add your handling code here:
        barraEstado("Selecciona el Codigo del \u00c1rea Tematica");
    }//GEN-LAST:event_codigAreaFocusGained

    private void codigAreaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_codigAreaMouseEntered
// TODO add your handling code here:
          barraEstado("Selecciona el Codigo del \u00c1rea Tematica");
    }//GEN-LAST:event_codigAreaMouseEntered

    private void botonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAyudaActionPerformed
// TODO add your handling code here:
         new MostrarAyuda();
    }//GEN-LAST:event_botonAyudaActionPerformed

    private void codigAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codigAreaActionPerformed
// TODO add your handling code here:
       BuscarArea();
    }//GEN-LAST:event_codigAreaActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
         this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        validaDatos();
    }//GEN-LAST:event_botonGuardarActionPerformed
    private void BuscarArea()
    {
         if (codigArea.getSelectedIndex()!=-1){
            try{
               Vector datosA = datbase.ConsultaIndividual("select areanombre, areatipo from codarea where areacodigo ="+codigArea.getSelectedItem());
               nombreArea.setText(datosA.elementAt(0).toString().trim());               
               jCBtipoArea.setSelectedItem(datosA.elementAt(1));
            } catch (Exception e) {
                javax.swing.JOptionPane.showMessageDialog(this,"ERROR PARAMETRO NO ENCONTRADO","ERROR!!",javax.swing.JOptionPane.ERROR_MESSAGE);
                
            }
            
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
         try{
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {}
         
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaModificaArea(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
      /**
     * metodo que envia a la clase  JDBCAdapter, el conector para la base dee datos, el nombre del
     * administrador de la misma, la clave y el driver.
     */
    private void conectar() {
        datbase = new JDBCAdapter(
            "jdbc:postgresql://localhost:5432/everest",
            "org.postgresql.Driver",
            "postgres",
            "root");
    }
     private void validaDatos() {
         if(!validarCamposVacios()){
              NuevaArea newArea = new NuevaArea();
             newArea.actualizaPrograma(codigArea.getItemAt(codigArea.getSelectedIndex()).toString(),nombreArea.getText().toString(),jCBtipoArea.getSelectedItem().toString());
             limpiar();
         }
         else
             javax.swing.JOptionPane.showMessageDialog(this,"No pueden quedar Campos Vacios","Error",0);
         
    }
private boolean validarCamposVacios() {
        if((jCBtipoArea.getSelectedIndex()==-1) || (nombreArea.getText().length()==0))
            return true;      
        
       else return false;
    }
   
private void barraEstado(String mensaje)
      {
          jLEstado.setText(mensaje);
      }

  private void limpiar(){
        
        codigArea.setSelectedIndex(0);
        nombreArea.setText("");        
        codigArea.requestFocus();
        BuscarArea();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTFecha;
    private javax.swing.JButton botonAyuda;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JComboBox codigArea;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jCBtipoArea;
    private javax.swing.JLabel jLEstado;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLlogo;
    private javax.swing.JPanel jPEncabezado;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTUsuario;
    private javax.swing.JTextField nombreArea;
    // End of variables declaration//GEN-END:variables
     JDBCAdapter datbase;  
      DecimalFormat df1 = new DecimalFormat("000");
       Fecha FechaAct= new Fecha(); 
}
